/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package repository;

import Settings.BaseSettings;
import java.io.File;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import Settings.ISettings;
import java.net.InetAddress;
import java.util.Objects;

/**
 *
 * @author Tomasz Nowak
 */
@Root
public class RepositoryProxySettings extends BaseSettings implements ISettings
{
	@Element
	private String database;

	@Element
	private String username;

	@Element
	private String password;

	@Element
	private InetAddress address;

	@Element
	private int port;

	@Override
	public int hashCode()
	{
		int hash = 5;
		return hash;
	}

	@Override
	public boolean equals( Object obj )
	{
		if ( obj == null )
		{
			return false;
		}
		if ( getClass() != obj.getClass() )
		{
			return false;
		}
		final RepositoryProxySettings other = ( RepositoryProxySettings ) obj;
		if ( !Objects.equals(this.filename, other.filename) )
		{
			return false;
		}
		if ( !Objects.equals(this.database, other.database) )
		{
			return false;
		}
		if ( !Objects.equals(this.username, other.username) )
		{
			return false;
		}
		if ( !Objects.equals(this.password, other.password) )
		{
			return false;
		}
		if ( !Objects.equals(this.address, other.address) )
		{
			return false;
		}
		return this.port == other.port;
	}

	public String getDatabase()
	{
		return database;
	}

	public void setDatabase( String database )
	{
		this.database = database;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername( String username )
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword( String password )
	{
		this.password = password;
	}

	public InetAddress getAddress()
	{
		return address;
	}

	public void setAddress( InetAddress address )
	{
		this.address = address;
	}

	public int getPort()
	{
		return port;
	}

	public void setPort( int port )
	{
		this.port = port;
	}

	public RepositoryProxySettings()
	{
		this.filename = "repositoryproxysettings.xml";
	}

	@Override
	public void load()
	{
		try
		{
			Serializer serializer = new Persister();
			File source = new File(this.filename);
			RepositoryProxySettings settings = serializer.read(RepositoryProxySettings.class, source);

			this.setAddress(settings.getAddress());
			this.setDatabase(settings.getDatabase());
			this.setPassword(settings.getPassword());
			this.setPort(settings.getPort());
			this.setUsername(settings.getUsername());
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
	}

	@Override
	public void save()
	{
		try
		{
			Serializer serializer = new Persister();
			File destination = new File(this.filename);
			serializer.write(this, destination);
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
	}
}
